import pandas as pd
import sys
import numpy as np
import matplotlib
import scipy
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
import seaborn as sns


def get_elapsed_time(lines, type):
  time_list = []
  for line in lines:
    if 'elapsed' in line and type in line:
      time_list.append(float(line.split(':')[4].strip().split(' ')[0]))
  return time_list


def get_elapsed_time_and_timestamp(filename, type):
  time_list = []
  timestamp_list = []

  try:
    with open(filename, 'r') as fopen:
      lines = fopen.readlines()
      for line in lines:
        if 'elapsed' in line and type in line:
          time_list.append(float(line.split(':')[4].strip().split(' ')[0]))
          ts = "2019-08-02 " + line.split(' ')[1]
          timestamp_list.append(pd.Timestamp(ts))
  except Exception as e:
    print(e)
  return pd.DataFrame({'time': time_list}, index=timestamp_list)


def plot_from_file(filename, type):
  try:
    with open(filename, 'r') as fopen:
      lines = fopen.readlines()

      time_list = get_elapsed_time(lines, type)

      df = pd.DataFrame({'time': time_list})
      print("filename: " + filename)
      print("MIN : %f" % df.min()[0])
      print("MAX : %f" % df.max()[0])
      print("COUNT: %d" % df.count())
      print("MEAN : %f" % df.mean()[0])
      print(df.quantile([.25, .5, .75, 0.9]))
      print()
  except Exception as e:
    print(e)


def get_elapsed_time_from_file(filename, type):
  with open(filename, 'r') as fopen:
    lines = fopen.readlines()
    time_list = get_elapsed_time(lines, type)
    return time_list


def cdf(data, limits="auto", npoints=600):
  kde = scipy.stats.gaussian_kde(data)
  bw = kde.factor
  if limits == "auto":
    limits = (data.min(), data.max())
  limits = (limits[0] - bw * np.diff(limits)[0], limits[1] + bw * np.diff(limits)[0])
  x = np.linspace(limits[0], limits[1], npoints)
  y = [kde.integrate_box(x[0], x[i]) for i in range(len(x))]
  return x, np.array(y)


def draw_cdf():
  d1 = np.array(get_elapsed_time_from_file("../log/deribit_paris_new.log", type))
  d2 = np.array(get_elapsed_time_from_file("../log/deribit_frankfrut_new.log", type))

  mini = np.min((d1.min(), d2.min()))
  maxi = np.max((d1.max(), d2.max()))

  x1, y1 = cdf(d1, limits=(mini, maxi))
  x2, y2 = cdf(d2, limits=(mini, maxi))

  f, ax = plt.subplots()

  ax.plot(x1, y1, label='paris')
  ax.plot(x2, y2, label='frankfrut')
  ax.legend(loc="upper right")

  plt.savefig('output.png')


def draw_cdf2(type):
  df1 = get_elapsed_time_and_timestamp("../log/deribit_paris_new.log", type)
  d1 = df1.rolling('50s').mean().time.values
  df2 = get_elapsed_time_and_timestamp("../log/deribit_frankfrut_new.log", type)
  d2 = df2.rolling('50s').mean().time.values

  mini = np.min((d1.min(), d2.min()))
  maxi = np.max((d1.max(), d2.max()))

  x1, y1 = cdf(d1, limits=(mini, maxi))
  x2, y2 = cdf(d2, limits=(mini, maxi))

  f, ax = plt.subplots()

  ax.plot(x1, y1, label='paris')
  ax.plot(x2, y2, label='frankfrut')
  ax.legend(loc="upper right")

  plt.savefig('output_ma.png')

  # import pdb;
  # pdb.set_trace()
  # plt.plot(df1.loc[df1['time'] < 0.03].rolling('200s').mean(), 'r-', lw=0.5);
  # plt.twinx();
  # plt.plot(df2.loc[df2['time'] < 0.03].rolling('200s').mean(), 'b-', lw=0.5);
  # plt.savefig("sung.png");
  # plt.close()


def main(argv):
  # filename = argv[1]
  type = 'submit'
  # draw_cdf()
  # plot_from_file("../log/deribit_paris_new.log", type)
  # plot_from_file("../log/deribit_frankfrut_new.log", type)
  # df = get_elapsed_time_and_timestamp("../log/deribit_paris_new.log", type)
  # print(timestamp_list)
  # print(df.head())
  # print(df.rolling('50s').mean().head())
  # print(df.rolling('50s').mean().count())
  draw_cdf2(type)


if __name__ == '__main__':
  main(sys.argv)
